<?php
define('API_KEY','329c5e271ace1d9671054629115fd97a');
class ApiAction extends BaseAction {
    public function __construct(){
        $api_key = $this->_post('api_key');
        if($api_key != API_KEY){
            exit('API_KEY_ERROR');
        }
    }

    private function ajax_status($status,$info){
        $array = array('status'=>$status,'info'=>$info);
        exit(json_encode($array));
    }

    public function sync_register(){
        $email = $this->_post('email');
        $password = $this->_post('password');
        $province = $this->_post('province');
        $city = $this->_post('city');
        $mobile = $this->_post('mobile');
        $ex_real_name = $this->_post('ex_real_name');
        $data = array();
        if(empty($email)){
            $this->ajax_status('0','email不能为空');
        }
        $data['email'] = $email;
        $data['user_name'] = $email;
        if(!empty($password)){
            $data['user_pwd'] = md5($password);
        }
        if(!empty($province) && !empty($city)){
            $data['province'] = $province;
            $data['city'] = $city;
        }
        if(!empty($mobile)){
            $data['mobile'] = '';
        }
        if(!empty($ex_real_name)){
            $data['ex_real_name'] = $ex_real_name;
        }
        $data['is_effect'] = '1';
        if( M('user')->where(array('email'=>$email))->find()){
            $data['update_time'] = time();
            $status = M('user')->where(array('email'=>$email))->data($data)->save();
        }else{
            $data['user_type'] = '';
            $data['create_time'] = time();
            $status = M('user')->data($data)->add();
        }

        if($status){
            $this->ajax_status('1','注册成功');
        }else{
            $this->ajax_status('0','注册失败');
        }
    }


    public function index(){
        exit('ACCESS DENY');
    }
}